package com.ym.soso.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ym.base.enums.FlagEnum;
import com.ym.common.utils.StringUtil;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.domain.param.PolyclinicSimQueryParam;
import com.ym.medical.domain.vo.PolyclinicSimVO;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.service.ISysCodeRuleSV;
import com.ym.soso.dao.PolyclinicDao;
import com.ym.soso.domain.entity.PolyclinicEntity;
import com.ym.soso.domain.param.PolyclinicQueryParam;
import com.ym.soso.domain.param.PolyclinicUpdateParam;
import com.ym.soso.domain.vo.PolyclinicListPcVO;
import com.ym.soso.service.IPolyclinicSV;
import com.ym.soso.service.search.service.PolyclinicIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
* 综合门诊 Service业务层处理
*
* @author ym
* @date 2020-09-02
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class PolyclinicSVImpl extends BaseSVImpl<PolyclinicEntity,PolyclinicDao> implements IPolyclinicSV {

	@Autowired
	private PolyclinicIndexService polyclinicIndexService;

	@Autowired
	private ISysCodeRuleSV sysCodeRuleSV;

    /**
     * 查询综合门诊 列表
     *
     * @param queryParam 综合门诊  查询参数
     * @return 综合门诊
     */
    @Override
    public List<PolyclinicListPcVO> selectListPC(PolyclinicQueryParam queryParam) {
        queryParam.setDeleted(FlagEnum.NO.getKey());
        List<PolyclinicListPcVO> polyclinics = mapper.selectListPC(queryParam);
        return polyclinics;
    }

    /**
     * 新增综合门诊
     *
     * @param updateParam 综合门诊  新增参数
     * @return 结果
     */
    @Override
    public Long insertPC(PolyclinicUpdateParam updateParam) {
        PolyclinicEntity entity = updateParam.covert();
        entity.setCreSb(ShiroUtil.getLoginId(true));
        entity.setTheCode(sysCodeRuleSV.createCode(CodeRuleEnum.COMPREHENSIVE_CLINIC));
        Long result = this.insert(entity);

        if (result > 0) {
            PolyclinicEntity polyclinicEntity = selectById(entity.getId());
            polyclinicIndexService.saveOrUpdate(polyclinicEntity);
        }
        return entity.getId();
    }

    /**
     * 修改综合门诊
     *
     * @param updateParam 综合门诊  修改参数
     * @return 结果
     */
    @Override
    public int updateByIdPC(PolyclinicUpdateParam updateParam) {
        PolyclinicEntity entity = updateParam.covert();
        entity.setUptSb(ShiroUtil.getLoginId(true));
        int result = this.updateById(entity);

        if(result > 0){
            PolyclinicEntity polyclinicEntity = selectById(updateParam.getId());
            polyclinicIndexService.saveOrUpdate(polyclinicEntity);
        }
        return result;
    }

    @Override
    public int delete(Long[] ids) {
        int result = updateByParams(PolyclinicEntity.builder().deleted(FlagEnum.IS.getKey()).build(),
                TK.selectWhere(PolyclinicEntity.class).andIn(PolyclinicEntity::getId, ids).end());

        if(result > 0){
            polyclinicIndexService.deleteAndSwapHeight(ids);
        }
        return result;
    }

    @Override
    public List<PolyclinicSimVO> selectListMobile(PolyclinicSimQueryParam queryParam) {
        List<Long> longs = polyclinicIndexService.searchByKeywordOfIds(queryParam);
        if (StrUtil.isNotBlank(queryParam.getKeyword())&&longs.isEmpty()) {
            return Collections.emptyList();
        }
        if(!longs.isEmpty()){
            Long[] ids = longs.toArray(new Long[longs.size()]);
            queryParam.setIds(ids);
        }
        if(StringUtil.isNotBlank(queryParam.getKeyword())){
            queryParam.setKeywordSort(true);
        }
        return mapper.selectListMobile(queryParam);
    }
}
